import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class SharedDataService {

  private sharedData: any;
  constructor() { 

  }
  // 保存共享数据
  setData(data: string) {
    this.sharedData = data;
  }
  // 取得共享数据
  getData(): string {
    return this.sharedData;
  }
  // 创建一个可观察对象
  public updateNeeded$ = new Subject<string>();

  triggerUpdate() {
    // 分发通知给订阅者
    this.updateNeeded$.next(this.getData());
  }
}
